<%@page import="java.sql.Connection" import="java.sql.Statement" import="java.sql.ResultSet" import="connection.DBConnection" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Customer Master Creation</title>

<script type= "text/javascript" src ="JS/State.js">
</script>

<script language="javascript" type="text/javascript">

function check()
{
	var c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11;
	
  	var namevalid=/^[a-zA-Z ]+$/;
  
        c1= document.f1.customername.value;

        if(c1.length==0)
        {
        	alert("Enter Customer Name");
            return false;
      	}
 /*       else if(c1.search(namevalid)==-1)
        {
            alert("Please Enter valid name");
            document.f1.customername.value="";
            document.f1.customername.focus();
            
	         return false;
    	 }
  */       
         c3=document.f1.customeraddress.value ;
         if(c3.length==0)
         {
              alert("Please Enter Address");
              return true;
          }

	      c4= document.f1.customercity.value;
          if(c4.length == 0)
          {
              alert("Please Enter City");
              return false;
          }

         c5=document.f1.customerstate.value;

         if(c5.length == 0)
         {
              alert("Please Enter State");
              return false;
         }

         c6= document.f1.customertin.value;
         if(c6.length == 0)
         {
              alert("Please Enter TIN Number");
              return true;
         }

         c7= document.f1.customerpan.value;
         if(c7.length == 0)
         {
              alert("Please Enter PAN Number");
              return false;
         }
         
         var PAN_REGEX= "^([a-zA-Z]){5}([0-9]){4}([a-zA-Z]){1}$";
         
         if(!c7.match(PAN_REGEX))
         {
        	 alert("Please Enter PAN Number in right format. For ex- AAAAA1111A")
        	 return true;
         }

         c8= document.f1.customermobile.value;
         if(c8.length == 0)
         {
             alert("Please Enter Mobile Number");
             return false;
         }
         
         if(c8.indexOf(" ")!=-1)
    	 {
       		alert("Invalid Mobile No.");
      		return false;
    	 }
    	 else if(c8.length>10)
     	 {
         	alert("Mobile Number should be 10 digit");
       		return true;
     	 }

  /*  	else  if (!(c8.charAt(0)=="9" || c8.charAt(0)=="8" || c8.charAt(0)=="7"))
     	{
        	 alert("Mobile No. should start with 9 or 8 or 7");
       		//  document.getElementById('empcontno').focus();
        	 return false;
     	}
  */
  		else  if (c8.charAt(0)=="0")
		{
  	 		alert("Mobile Number should not start with 0");
 			//  document.getElementById('empcontno').focus();
 			 return true;
		}
  		else if(!(c8.length==10))
     	{
        	 alert("Mobile No. should be 10 digit");
        	// document.getElementById('empcontno').focus();
        	 return true;
     	}

         
         c9= document.f1.customeremail.value;
         
         var regex = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
         
         if(regex.test(c9) == false)
         {
              alert("Invalid Email Address");
              return true;
          }
   
         c10= document.f1.customercontactperson.value;
         if(c10.length==0)
         {
         	alert("Enter Contact Person Name");
             return true;
       	 }
 /*        else if(c10.search(namevalid)==-1)
         {
             alert("Please Enter valid name");
             document.f1.customercontactperson.value="";
             document.f1.customercontactperson.focus();
             
 	         return false;
     	 }
  */       
         c11= document.f1.copeningbalance.value;
         if(c11.length==0)
         {
         	alert("Enter Opening Balance");
             return false;
       	 }
         
         if(!c11.match(/^\d+$/))
       	 {
        	 alert("Please Enter Opening Balance in Numerics only");
             return false;
       	 }
      
        return true;
   }

</script>

</head>

<body >

<form  name="f1" action="CustomerMasterServlet" ONSUBMIT="return check()">
<center><h2> Customer Master </h2></center>
<table align="center">
	
<tr>
	<th>	</th>
	<th>	</th>
</tr>

<tr>
	<td> Customer Name </td>
	<td> <input type="text" name="customername"> </td>
</tr>

<tr>
	<td> Customer Code </td>
	
				<%	DBConnection dbc=new DBConnection();	
				 	Connection con=dbc.getNewConnection();
				 	String code2="C1";
			
				 Statement st = null;
				 ResultSet rs = null;
			try
			{
			   st=con.createStatement() ;
       		   rs=st.executeQuery("select ccode from CustomerMaster"); %>
       		   
       		 <%  while(rs.next()) 
       		 	{ 
       				String code1=rs.getString(1);
       				
       			   String[] part = code1.split("(?<=\\D)(?=\\d)");
	   		       
	   		       String p0=part[0];		       
	   		       int p1=Integer.parseInt(part[1]);
	   		      
	   		       p1++;
	   		       
	   		       code2=p0+""+p1;

            	} %>
  
	 		<% }
		   catch(Exception e){} %>
		
	<td> <input type="text" name="customercode" value="<%=code2 %>" > </td>
</tr>

<tr>
	<td> Address</td>
	<td> <input type="text" name="customeraddress"> </td>
</tr>

<tr>
	<td> State </td>
	<%-- <td> <input type="text" name="customerstate"> </td> --%>
	<td> <select id="state" name ="state"></select> </td>
</tr>

<tr>
	<td> City </td>
	<%-- <td> <input type="text" name="customercity"> </td> --%>
	<td> <select name ="city" id ="city"></select> </td>
</tr>

<tr>
	<td> TIN Number </td>
	<td> <input type="text" name="customertin"> </td>
</tr>

<tr>
	<td> PAN Number</td>
	<td> <input type="text" name="customerpan"> </td>
</tr>

<tr>
	<td> Mobile Number</td>
	<td> <input type="text" name="customermobile"> </td>
</tr>

<tr>
	<td> Email ID </td>
	<td> <input type="text" name="customeremail"> </td>
</tr>

<tr>
	<td> Contact Person Name </td>
	<td> <input type="text" name="customercontactperson"> </td>
</tr>

<tr>
	<td> Opening Balance </td>
	<td> <input type="text" name="copeningbalance"> </td>
</tr>

<tr>
	
	<td> <input type="submit" name="enter" value="Submit"> </td>
</tr>

</table>


</form>

 <script language="javascript">
	populateStates("state", "city");
 </script>

</body>
</html>
